package org.hs.phd.odi.tracking.tool.performanceevaluation;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.text.DecimalFormat;
import java.util.IdentityHashMap;

import org.hs.phd.common.simpleplayer.Overlay;
import org.hs.phd.odi.tracking.performanceevaluation.TrackingPerformanceMetric;
import org.hs.phd.odi.tracking.performanceevaluation.TrackingPerformanceStatistics;
import org.hs.phd.odi.tracking.tracker.TrackerGroup.PaintableTracker;

public class PerformanceStatsOverlay implements Overlay {

	private final IdentityHashMap<PaintableTracker, TrackingPerformanceStatistics> performanceStatistics;
	private DecimalFormat decimalFormat;
	private boolean isDetailsOverlayEnabled;

	public PerformanceStatsOverlay(IdentityHashMap<PaintableTracker, TrackingPerformanceStatistics> performanceStatistics) {
		this.performanceStatistics = performanceStatistics;
		decimalFormat = new DecimalFormat("#####.##");
		isDetailsOverlayEnabled = false;
	}

	@Override
	public void paint(Graphics g) {
		
		int xStart = 10;
		int x = xStart;
		int y = 10;
		
		Graphics2D g2 = (Graphics2D) g;
		FontMetrics fm = g2.getFontMetrics();
		
		int linespacing = fm.getHeight() + 3;
		
		for( PaintableTracker tracker : performanceStatistics.keySet()){
			
			x = xStart;
			String trackerName = tracker.tracker.getName();
			
			g2.setPaint(tracker.paint);
			g2.drawString(trackerName, x, y);
			
			y += linespacing;

			if(isDetailsOverlayEnabled()){
				for( TrackingPerformanceMetric metric :  performanceStatistics.get(tracker).iterator()){
					x = xStart;
					String metricShortName = metric.getShortName() + ": ";
					g2.drawString(metricShortName, x, y);
	
					x += fm.stringWidth(metricShortName);
					g2.drawString( decimalFormat.format(metric.getLastValue())
							+ " (" + decimalFormat.format(metric.getAverageValue()) + ")", x, y);
					
					y += linespacing;
					
				}
			}
			
		}
		
		
		
		
		
		
	}

	public boolean isDetailsOverlayEnabled() {
		return isDetailsOverlayEnabled;
	}

	public void setDetailsOverlayEnabled(boolean isDetailsOverlayEnabled) {
		this.isDetailsOverlayEnabled = isDetailsOverlayEnabled;
	}

}
